<?php
function Paging($page, $rowCount, $rowsOfPage, $nextText, $prevText, $btnGoText, $linkCls, $textCls, $inputCls, &$note, $formName) {	
	$btnGoToPage = $btnGoText;
	$btnNextPage = $nextText;
	$btnPrevPage = $prevText;	
	$returnData = "";	$notes	= "";	
	if ($rowCount>0) {		
		$pages = ceil($rowCount/$rowsOfPage);		
		if ($page>1)  			
			$prev = "<a href='javascript: _goPage($page, -1,".$formName.");' class='$linkCls' title='$btnGoToPage ".($page-1)."'>$btnPrevPage</a>"; 		
		else 			
			$prev = "<span class='$textCls'>$btnPrevPage</span>";		
		if ($page*$rowsOfPage < $rowCount) 			
			$next = "<a href='javascript: _goPage($page, 1,".$formName.");' class='$linkCls' title='$btnGoToPage ".($page+1)."'>$btnNextPage</a>"; 		
		else 			
			$next = "<span class='$textCls'>$btnNextPage<span>";		
		$returnData = "$prev <span class='$textCls'>|</span> $next&nbsp;&nbsp;&nbsp;";		
		$returnData .= "<a href=\"javascript: _go(".$formName.");\">$btnGoToPage</a>&nbsp;";		
		$returnData .="<input name=numGo type=text size=2 maxlength=3 class='$inputCls'>&nbsp;&nbsp;<span class='$textCls'> $page/$pages</span>";		
		$note = "<span class='$textCls'>".(($page-1)*$rowsOfPage+1)."-".($page*$rowsOfPage>$rowCount?$rowCount:$page*$rowsOfPage);	
	}	
	if (ceil($rowCount/$rowsOfPage)<2) return "";
	else return $returnData;
}

function PagingGroup($page, $rowCount, $rowsOfPage, $itemsOfGroup, $linkCls, $textCls, $inputCls, &$note, $formName) {	
	$pages =  ceil($rowCount/$rowsOfPage);	
	$numGroup = ceil($pages/$itemsOfGroup);	
	if ($page>$pages) $page = $pages;	
	$inGroup = ceil($page/$itemsOfGroup);	
	$returnData = "";	$notes	= "";	
	if ($rowCount>0) {		
		for ($i=($inGroup-1)*$itemsOfGroup+1; $i<=($inGroup-1)*$itemsOfGroup+$itemsOfGroup && $i<=$pages; $i++) {
			if ($i==$page)				
				$returnData .= "<span class='$textCls'><font color='red'>$i</font></span> ";			
			else				
				$returnData .= "<a href='javascript: _goPage($i-1, 1,".$formName.");' class='$linkCls' title='Go to page $i'><b>$i</b></a> ";		
		}		
		if ($returnData!="") {			
			if ($numGroup>1) {				
				if ($inGroup>1)					
					$returnData = "<b>[</b><a href=\"javascript: _goPage($page-$itemsOfGroup-1, 1,".$formName.");\" class='$linkCls' title='Jump to previous $itemsOfGroup pages'><b>Previous  $itemsOfGroup</b></a><span class='$textCls'><b>] </b></span>".$returnData;				
				else 					
					$returnData = "<span class='$textCls'> [Previous  $itemsOfGroup] </span><span class='$textCls'> </span>".$returnData;				
				if ($inGroup<$numGroup)					
					$returnData .= "<span class='$textCls'><b> [</b></span><a href=\"javascript: _goPage($page+$itemsOfGroup-1, 1,".$formName.");\" class='$linkCls' title='Jump next to $itemsOfGroup pages'><b>Next  $itemsOfGroup</b></a><b>] </b>";				
				else 					
					$returnData .= "<span class='$textCls'><b> </b></span><span class='$textCls'> [Next  $itemsOfGroup] </span>";			
			}			
			if ($page>1)				
				$returnData = " <b>[</b><a href=\"javascript: _goPage(0, 1,".$formName.");\" class='$linkCls' title='Go to page 1'><b>First</b></a><span class='$textCls'><b>] </b></span>".$returnData;			
			else 				
				$returnData = "<span class='$textCls'> [First] </span><span class='$textCls'><b>  </b></span>".$returnData;			
			if ($page<$pages)				
				$returnData .= " <b>[</b><a href=\"javascript: _goPage($pages-1, 1,".$formName.");\" class='$linkCls' title='Go to page $pages'><b>Last</b></a><span class='$textCls'><b>] </b></span>";			
			else 				
				$returnData .= "  <span class='$textCls'> [Last]</span><span class='$textCls'></span>";		
		}		
		$note = "<span class='$textCls'>Records from ".(($page-1)*$rowsOfPage+1)."-".($page*$rowsOfPage>$rowCount?$rowCount:$page*$rowsOfPage)." of $rowCount";	
	}	
	if (ceil($rowCount/$rowsOfPage)<2) return "";
	else return $returnData;
}

function RowCount($db, $tableName, $whereStr, &$errorMsg, $joinTables="", $onFields="") {
	$errorMsg = "";	
	$rowCount = 0;
	$joinClause = "";
	for ($i=0; is_array($joinTables) && $i<count($joinTables); $i++)
	{
		$joinClause .= " LEFT JOIN ".$joinTables[$i]." ".$joinTables[$i]." ON ".$onFields[$i];
	}
	$query	= "SELECT COUNT('x') AS RowCount FROM $tableName $joinClause";		
	if ($whereStr!="") $query = $query." WHERE ".$whereStr;

	if (!$db->query($query)) 		
		$errorMsg	= mysql_error()." Query: $query.";		
	else if ($db->next_record()) 
			$rowCount = $db->f("RowCount");
		
	return $rowCount;
}


function PagingClient($page, $rowCount, $rowsOfPage, $nextText, $prevText, $btnGoText, $linkCls, $textCls, $inputCls, &$note, $formName, $numObj) {	
	$btnGoToPage = $btnGoText;
	$btnNextPage = $nextText;
	$btnPrevPage = $prevText;	
	$returnData = "";	$notes	= "";	
	if ($rowCount>0) {		
		$pages = ceil($rowCount/$rowsOfPage);		
		if ($page>1)  			
			$prev = "<a href='javascript: _goPage($page, -1,".$formName.");' class='$linkCls' title='$btnGoToPage ".($page-1)."'>$btnPrevPage</a>"; 		
		else 			
			$prev = "<span class='$textCls'>$btnPrevPage</span>";		
		if ($page*$rowsOfPage < $rowCount) 			
			$next = "<a href='javascript: _goPage($page, 1,".$formName.");' class='$linkCls' title='$btnGoToPage ".($page+1)."'>$btnNextPage</a>"; 		
		else 			
			$next = "<span class='$textCls'>$btnNextPage<span>";		
		$returnData = "$prev <span class='$textCls'>|</span> $next&nbsp;&nbsp;&nbsp;";		
		$returnData .= "<a href=\"javascript: _goClient(".$formName.", $formName.numGo$numObj.value);\">$btnGoToPage</a>&nbsp;";		
		$returnData .="<input name=numGo$numObj type=text size=2 maxlength=3 class='$inputCls'>&nbsp;&nbsp;<span class='$textCls'> $page/$pages</span>";		
		$note = "<span class='$textCls'>".(($page-1)*$rowsOfPage+1)."-".($page*$rowsOfPage>$rowCount?$rowCount:$page*$rowsOfPage);	
	}	
	if (ceil($rowCount/$rowsOfPage)<2) return "";
	else return $returnData;
}
?>